package com.nb.fly.repository.impl;

import com.nb.fly.repository.ShopRepository;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${NMAE}
 *
 * @author Gwg
 * @date 2020/6/28
 **/
@Repository
@Slf4j
public class ShopRepositoryImpl implements ShopRepository {

      @Autowired
      private RestHighLevelClient restHighLevelClient;

      /**
       * 查询列表
       * @param request
       * @return
       * @throws Exception
       */
      @Override
      public Map<String, Object> shopList(SearchRequest request) throws  Exception{
            log.info("search shop list dsl -> {}", request.source().toString());
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHits hits = response.getHits();
            List<Map<String, Object>> shopList = new ArrayList<>();
            for (SearchHit hit : hits) {
                  shopList.add(hit.getSourceAsMap());
            }
            Map<String, Object> result = new HashMap<>();
            result.put("list",shopList);
            result.put("scrollId",response.getScrollId());
            log.info("search shop list total -> {}", hits.totalHits);
            return result;
      }

      /**
       * 查询列表
       * @param request
       * @return
       * @throws Exception
       */
      @Override
      public Map<String, Object> shopList(SearchScrollRequest request) throws Exception{
            log.info("search shop scroll list dsl -> {}", request.toString());
            SearchResponse searchResponse = restHighLevelClient.scroll(request, RequestOptions.DEFAULT);
            SearchHits hits = searchResponse.getHits();
            List<Map<String, Object>> hospitalList = new ArrayList<>();
            for (SearchHit hit : hits) {
                  hospitalList.add(hit.getSourceAsMap());
            }
            Map<String, Object> result = new HashMap<>();
            result.put("list", hospitalList);
            result.put("scrollId", searchResponse.getScrollId());
            log.info("search shop scroll list total -> {}", hits.totalHits);
            return result;
      }

      @Override
      public void update(UpdateRequest request) throws Exception{
            log.info("update shop request -> {}", request.toString());
            UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
            log.info("update shop status -> {}", update.status().getStatus());
      }
}
